/**
 * 作者: sessionliang
 * 时间: 2017-08-20
 * 描述: 学时详情页面
 */
define(function(require, exports, module) {
	"use strict";
	//每一个页面都要引入的工具类
	var CommonTools = require('CommonTools_Core');
	var WindowTools = require('WindowTools_Core');
	//轮播图工具类
	var GallerySliderTools = require('GallerySliderTools_Core');

	//base service
	var baseService = require('Base_Service');
	var trainService = require('Train_Service');
	var orderService = require('Order_Service');
	var enumHelper = require('Common_Enum');
	
	var model = null;

	//每一个页面都要引入的工具类
	// initready 要在所有变量初始化做完毕后
	CommonTools.initReady(initData);
	/**
	 * @description 初始化数据,结合initReady使用
	 * plus情况为plusready
	 * 其它情况为直接初始化
	 */
	function initData() {
		//引入必备文件,下拉刷新依赖于mui与mustache
		CommonTools.importFile([
			'js/libs/mui.min.js'
		], function() {
			initHomePage();
			initListeners();
		});
	}

	/**
	 * @description 监听
	 */
	function initListeners() {
		mui(".mui-content").on("tap", "#btnWaiting",function(){
			if(model!=null){
				orderService.pay(model.Code,function(retval){
					initHomePage();
				});
			}
		});
		mui(".mui-content").on("tap","#btnCancel",function(){
			if(model!=null){
				mui.confirm("确定要取消吗？",function(){
					orderService.cancel(model.Code,function(retval){
						initHomePage();
					});
				});
			}
		});
	}

	/**
	 * @description 渲染頁面
	 */
	function initHomePage() {
		var id = WindowTools.getExtraDataByKey("mid");
		trainService.getTrain(id, function(retval) {
			model = retval;
			mui("#Code")[0].innerHTML = retval.Code;
			mui("#DrivingTitle")[0].innerHTML = retval.DrivingTitle;
			mui("#PlaceName")[0].innerHTML = retval.PlaceName;
			mui("#DrivingLicenseType")[0].innerHTML = enumHelper.drivingLicenseTypeFormat(retval.DrivingLicenseType);
			mui("#CourseType")[0].innerHTML = enumHelper.courseTypeFormat(retval.CourseType);
			mui("#TimeSoltType")[0].innerHTML = enumHelper.timeSoltTypeFormat(retval.TimeSoltType);
			mui("#TotalHours")[0].innerHTML = retval.TotalHours;
			mui("#UsedHours")[0].innerHTML = retval.UsedHours;
			mui("#Status")[0].innerHTML = enumHelper.orderStatusTypeFormat(retval.Status);
			//是否支付
			if(retval.Status == "Waiting"){
				//待支付
				mui("#btnWaiting")[0].className = "mui-btn mui-btn-primary mui-btn-block";
				mui("#btnCancel")[0].className = "mui-btn mui-btn-danger mui-btn-block";
			}
			else if(retval.Status == "Done"){
				mui("#btnResult")[0].innerHTML = enumHelper.orderStatusTypeFormat(retval.Status);
				mui("#btnResult")[0].className = "mui-btn mui-btn-success mui-btn-block";
				mui("#btnWaiting")[0].className = "mui-btn mui-hidden";
				mui("#btnCancel")[0].className = "mui-btn mui-hidden";
			}
			else if(retval.Status == "Cancel"){
				mui("#btnResult")[0].innerHTML = enumHelper.orderStatusTypeFormat(retval.Status);
				mui("#btnResult")[0].className = "mui-btn mui-btn-danger mui-btn-block";
				mui("#btnWaiting")[0].className = "mui-btn mui-hidden";
				mui("#btnCancel")[0].className = "mui-btn mui-hidden";
			}
		});
	}
});